<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/5/22
 * Time: 14:56
 */
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\AdminMenu;
use App\Models\AdminRole;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class UserController extends Controller{
    //管理员登录
    public function adminLogin(Request $request){
        try{
            $username = $request->input("username",false);
            $password = $request->input("password",false);
            if (!$username || !$password)return self::error(1000);
            $admin = Admin::where("username",$username)->first();
            if (!$admin)return self::error(1003);
            if (md5($password)!=$admin->password)return self::error(1004);
            Session::put("adminId",$admin->id);
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //一级菜单
    public function oneMenuApi(){
        try{
            $menu = AdminMenu::where("prev_id",0)->select("id","menu_name","prev_id")->get();
            return self::success($menu);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //查询菜单
    public function adminMenuApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $menuCount = AdminMenu::count();
            $menu = AdminMenu::skip(($page-1)*$size)->take($size)->orderBy('id','desc')->get();
            return self::jsondata($menuCount,$menu);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增菜单
    public function adminMenuAddApi(Request $request){
        try{
            $menuName = $request->input("menu_name");
            $prevId = $request->input("prev_id",false);
            $url = $request->input("url");
            if (!$menuName)return self::error(1000);
            $menu = new AdminMenu();
            $menu->menu_name = $menuName;
            $menu->prev_id = $prevId;
            $menu->url = $url;
            $menu->created_at = Carbon::now();
            $menu->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除菜单
    public function adminMenuDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $menu = AdminMenu::find($id);
            $menu->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //角色列表
    public function adminRoleApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $roleCount = AdminRole::count();
            $roleData = AdminRole::skip(($page-1)*$size)->take($size)->orderBy("id","desc")->get();
            return self::jsondata($roleCount,$roleData);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }


    //查询全部菜单
    public function adminMenuAllApi(){
        try{
            //查询一级菜单
            $oneMenu = AdminMenu::where("prev_id",0)->get();
            if ($oneMenu->count()<1)return self::error(1003);
            $menuArray = [];
            foreach ($oneMenu as $item){
                //查询二级菜单
                $secondMenu = AdminMenu::where("prev_id",$item->id)->get();
                $item['child'] = $secondMenu;
                $menuArray[] = $item;
            }
            return self::success($menuArray);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }


    //角色权限保存
    public function adminRoleAuthorizationSaveApi(Request $request){
        try{
            $id = $request->input("id",false);
            $menuId = $request->input("menu_id",false);
            if (!$id)return self::error(1000);
            $role = AdminRole::find($id);
            $menuArray = [];
            if ($menuId){
                $menuStr = explode(',',$menuId);
                foreach ($menuStr as $item){
                    $menu = AdminMenu::where('id',$item)->select('id','menu_name','prev_id','url')->first();
                    $menuArray[] = json_decode($menu,true);
                }
            }
            $oneMenu = [];
            foreach ($menuArray as $item){
                if ($item['prev_id']==0){
                    $oneMenu[] = $item;
                }
            }
            $array = [];
            foreach ($oneMenu as $item){
                foreach ($menuArray as $value){
                    if ($item['id']==$value['prev_id']){
                        $item['child'][] = $value;
                    }
                }
                $array[] = $item;
            }
            $role->resource = json_encode($array);
            $role->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //角色权限保存
    public function adminRoleAuthorizationAddApi(Request $request){
        try{
            $name = $request->input("name",false);
            $menuId = $request->input("menu_id",false);
            if (!$name)return self::error(1000);
            $menuArray = [];
            if ($menuId){
                $menuStr = explode(',',$menuId);
                foreach ($menuStr as $item){
                    $menu = AdminMenu::where('id',$item)->select('id','menu_name','prev_id','url')->first();
                    $menuArray[] = json_decode($menu,true);
                }
            }
            $oneMenu = [];
            foreach ($menuArray as $item){
                if ($item['prev_id']==0){
                    $oneMenu[] = $item;
                }
            }
            $array = [];
            foreach ($oneMenu as $item){
                foreach ($menuArray as $value){
                    if ($item['id']==$value['prev_id']){
                        $item['child'][] = $value;
                    }
                }
                $array[] = $item;
            }
            $role = new AdminRole();
            $role->name = $name;
            $role->resource = json_encode($array);
            $role->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //管理员列表
    public function userListApi(Request $request){
        try{
            $page = $request->input("page",1);
            $size = $request->input("limit",20);
            $count = Admin::count();
            $admin = Admin::skip(($page-1)*$size)->take($size)->get();
            return self::jsondata($count,$admin);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //角色列表
    public function roleAllApi(){
        try{
            $role = AdminRole::get();
            return self::success($role);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增管理员
    public function userAddApi(Request $request){
        try{
            $roleId = $request->input("role_id");
            $username = $request->input("username");
            $password = $request->input("password");
            $role = AdminRole::find($roleId);
            $admin = new Admin();
            $admin->role_id = $roleId;
            $admin->role_name = $role->name;
            $admin->username = $username;
            $admin->password = md5($password);
            $admin->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除角色
    public function roleDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $role = AdminRole::find($id);
            $role->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除管理员
    public function userDeleteApi(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $user = Admin::find($id);
            $user->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //修改密码
    public function userPasswordSaveApi(Request $request){
        try{
            $id = $request->input("id",false);
            $password = $request->input("password",false);
            if (!$id || !$password)return self::error(1000);
            $user = Admin::find($id);
            $user->password = md5($password);
            $user->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

}